package com.mylearn.myblog.controller;

import com.mylearn.myblog.entity.Tag;
import com.mylearn.myblog.service.TagService;
import com.mylearn.myblog.util.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/tag")
@RestController
@Api(value = "标签接口",description = "标签接口")
public class TagController {

	@Autowired
	private TagService tagService;

	@ApiOperation(value = "新增标签",notes = "新增标签")
	@RequestMapping(value = "/addtag",method = RequestMethod.POST)
	public BaseResult addtag(@RequestBody Tag tag){
		Tag tag1 = tagService.getTagByName(tag.getName());
		if (tag1 != null){
			return BaseResult.notOk();
		}
		Tag t = tagService.saveTag(tag);
		if (t == null){
			return BaseResult.notOk();
		}
		return BaseResult.ok(t);
	}

	@ApiOperation(value = "分页查询标签",notes = "分页查询标签")
	@RequestMapping(value = "/tags",method = RequestMethod.GET)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "第几页", dataType = "Integer", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页显示数", dataType = "Integer", paramType = "query", required = true)
	})
	public BaseResult tags(@RequestParam Integer page,@RequestParam Integer pageSize){
		if(page<1){
			return BaseResult.notOk();
		}
		Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.ASC,"id");
		Page<Tag> tags = tagService.listTag(pageable);
		return BaseResult.ok(tags);
	}

	@ApiOperation(value = "根据id查询标签",notes = "根据id查询标签")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "标签id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/findTagById/{id}",method = RequestMethod.GET)
	public BaseResult findTagById(@PathVariable Long id){
		return BaseResult.ok(tagService.getTag(id));
	}

	@ApiOperation(value = "修改/更新标签",notes = "修改/更新标签")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "标签id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/editTag/{id}",method = RequestMethod.POST)
	public BaseResult editTag(@PathVariable Long id,@RequestBody Tag tag){
		Tag tag1 = tagService.getTagByName(tag.getName());
		if (tag1 != null){
			return BaseResult.notOk();
		}
		Tag t = tagService.updateTag(id,tag);
		if (t == null){
			return BaseResult.notOk();
		}
		return BaseResult.ok(t);
	}

	@ApiOperation(value = "根据id删除标签",notes = "根据id删除标签")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "标签id", dataType = "long", paramType = "path", required = true)
	})
	@RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
	public BaseResult delete(@PathVariable Long id){
		Tag tag = tagService.getTag(id);
		if (tag != null){
			tagService.deleteTag(id);
			return BaseResult.ok();
		}
		return BaseResult.notOk();
	}

	@ApiOperation(value = "查询全部分类标签",notes = "查询全部标签")
	@RequestMapping(value = "/listAll",method = RequestMethod.GET)
	public BaseResult listAll(){
		return BaseResult.ok(tagService.listAll());
	}
}
